package com.wzy.oa_sys.service.impl.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzy.oa_sys.mapper.user.PositionMapper;
import com.wzy.oa_sys.model.pojo.user.OaPosition;
import com.wzy.oa_sys.model.request.user.PositionReq;
import com.wzy.oa_sys.model.response.user.PositionResp;
import com.wzy.oa_sys.service.user.PositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class PositionServiceImpl  extends ServiceImpl<PositionMapper, OaPosition> implements PositionService {

    @Autowired
    private PositionMapper positionMapper;
    @Override
    public List<PositionResp> getPositionList() {
        List<OaPosition> oaPositions = positionMapper.selectList(null);
        List<PositionResp> positionList =new ArrayList<>();
        oaPositions.forEach(oaPosition -> {
            positionList.add(PositionResp.builder()
                    .id(oaPosition.getNPositionId())
                    .position(oaPosition.getCPosition())
                    .desc(oaPosition.getCDescribtion())
                    .level(oaPosition.getNLevel())
                    .salary(oaPosition.getNSalary())
                    .build());
        });
        return positionList;
    }

    @Override
    public OaPosition getPositionById(long nPositionId) {
        return positionMapper.getPositionById(nPositionId);
    }

    @Override
    public void addOption(PositionReq positionReq) {
        OaPosition oaPosition = new OaPosition();
        oaPosition.setCPosition(positionReq.getPosition());
        oaPosition.setCDescribtion(positionReq.getDesc());
        oaPosition.setNLevel(positionReq.getLevel());
        oaPosition.setNSalary(positionReq.getSalary());
        positionMapper.insert(oaPosition);
    }

    @Override
    public int deleteOptionById(Integer id) {
        int i = positionMapper.deleteOptionById(id);
        return i;
    }

    @Override
    public void updateOption(PositionReq positionReq) {
        QueryWrapper<OaPosition> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("n_position_id",positionReq.getId());
        OaPosition oaPosition1 = positionMapper.selectOne(queryWrapper);
        if(positionReq.getPosition()!=null&&positionReq.getPosition()!=""){
            oaPosition1.setCPosition(positionReq.getPosition());
        }
        if(positionReq.getDesc()!=null&&positionReq.getDesc()!=""){
            oaPosition1.setCDescribtion(positionReq.getDesc());
        }
        if(positionReq.getLevel()!=0){
            oaPosition1.setNLevel(positionReq.getLevel());
        }
        if(positionReq.getSalary()!=null){
            oaPosition1.setNSalary(positionReq.getSalary());
        }
        positionMapper.update(oaPosition1,queryWrapper);
    }
}
